// 太阳能发电系统监控界面 - 设置页面脚本

// 初始化Vue应用
const vm = new Vue({
  el: "#app",
  data: {
    // 从全局数据获取
    lastUpdateTime: appData.lastUpdateTime,
    showAlert: appData.showAlert,
    alertMessage: appData.alertMessage,
    settingsData: { ...appData.settingsData },

    // 设置页面特定数据
    isLoading: false,
    savedMessage: "",
    showConfirmReset: false,

    // 版本信息
    appVersion: "v1.0.0",
    buildDate: "2023-05-13",
  },
  methods: {
    // 更新最后更新时间
    updateLastUpdateTime() {
      this.lastUpdateTime = appMethods.updateLastUpdateTime();
    },

    // 显示系统报警
    showSystemAlert(message) {
      appMethods.showSystemAlert(message);
      this.showAlert = appData.showAlert;
      this.alertMessage = appData.alertMessage;
    },

    // 确认报警
    acknowledgeAlert() {
      appMethods.acknowledgeAlert();
      this.showAlert = appData.showAlert;
    },

    // 切换自动刷新设置
    toggleAutoRefresh() {
      this.settingsData.autoRefresh = !this.settingsData.autoRefresh;
      this.saveSettings();
      this.showSavedMessage(
        "自动刷新已" + (this.settingsData.autoRefresh ? "开启" : "关闭")
      );
    },

    // 切换深色模式设置
    toggleDarkMode() {
      this.settingsData.darkMode = !this.settingsData.darkMode;
      // 使用全局方法应用暗色模式
      appMethods.toggleDarkMode();
      // 同步本地数据
      this.settingsData.darkMode = appData.settingsData.darkMode;
      this.showSavedMessage(
        "暗色模式已" + (this.settingsData.darkMode ? "开启" : "关闭")
      );
    },

    // 切换通知设置
    toggleNotifications() {
      this.settingsData.notificationsEnabled =
        !this.settingsData.notificationsEnabled;
      this.saveSettings();
      this.showSavedMessage(
        "通知提醒已" +
          (this.settingsData.notificationsEnabled ? "开启" : "关闭")
      );
    },

    // 更改刷新间隔时间
    changeRefreshInterval(increment) {
      let newValue = this.settingsData.refreshInterval + increment;

      // 限制范围：10秒 - 120秒
      if (newValue < 10) newValue = 10;
      if (newValue > 120) newValue = 120;

      this.settingsData.refreshInterval = newValue;
      this.saveSettings();
      this.showSavedMessage(`刷新间隔已设置为 ${newValue} 秒`);
    },

    // 更改每日目标值
    changeDailyTarget(increment) {
      let newValue = this.settingsData.dailyTargetValue + increment;

      // 限制范围：1 - 20度
      if (newValue < 1) newValue = 1;
      if (newValue > 20) newValue = 20;

      this.settingsData.dailyTargetValue = newValue;
      this.saveSettings();
      this.showSavedMessage(`每日发电目标已设置为 ${newValue} 度`);
    },

    // 切换温度单位
    toggleTemperatureUnit() {
      this.settingsData.temperatureUnit =
        this.settingsData.temperatureUnit === "celsius"
          ? "fahrenheit"
          : "celsius";
      this.saveSettings();
      const unitText =
        this.settingsData.temperatureUnit === "celsius" ? "摄氏度" : "华氏度";
      this.showSavedMessage(`温度单位已切换为 ${unitText}`);
    },

    // 显示重置确认对话框
    showResetConfirmation() {
      this.showConfirmReset = true;
    },

    // 取消重置
    cancelReset() {
      this.showConfirmReset = false;
    },

    // 确认重置
    confirmReset() {
      this.showConfirmReset = false;
      this.resetSettings();
    },

    // 重置所有设置
    resetSettings() {
      this.isLoading = true;

      // 模拟网络延迟
      setTimeout(() => {
        // 恢复默认设置
        this.settingsData = {
          autoRefresh: true,
          refreshInterval: 30,
          darkMode: false,
          notificationsEnabled: true,
          dailyTargetValue: 1.5,
          temperatureUnit: "celsius",
        };

        // 更新全局设置并保存
        Object.assign(appData.settingsData, this.settingsData);

        // 应用设置
        appMethods.applyDarkMode();

        // 显示成功消息
        this.showSavedMessage("设置已重置为默认值");

        this.isLoading = false;
      }, 800);
    },

    // 保存设置到全局状态
    saveSettings() {
      // 更新全局设置
      Object.assign(appData.settingsData, this.settingsData);

      // 保存到localStorage
      appMethods.saveSettingsToStorage();
    },

    // 显示保存成功消息
    showSavedMessage(message) {
      this.savedMessage = message;

      // 2秒后隐藏消息
      setTimeout(() => {
        this.savedMessage = "";
      }, 2000);
    },
  },
  mounted() {
    // 更新初始时间
    this.updateLastUpdateTime();

    // 确保从localStorage加载最新设置
    appMethods.loadSettingsFromStorage();

    // 从全局数据同步设置
    this.settingsData = { ...appData.settingsData };

    console.log("设置页面加载完成，当前设置:", this.settingsData);
  },
});
